package com.xzgj.fmmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzgj.fmmall.dao.ProductCommentsMapper;
import com.xzgj.fmmall.entity.ProductComments;
import com.xzgj.fmmall.service.ProductCommentsService;
import com.xzgj.fmmall.vo.ProductCommentsVO;
import com.xzgj.fmmall.vo.ResStatus;
import com.xzgj.fmmall.vo.ResultVO;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
@Service

public class ProductCommentsServiceImpl implements ProductCommentsService {

    @Resource
    private ProductCommentsMapper productCommentsMapper;

    @Override
    public PageInfo<ProductCommentsVO> selectProductCommentsByProductId(String productId, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<ProductCommentsVO> commentsVos = productCommentsMapper.selectProductCommentsByProductId(productId);
        PageInfo<ProductCommentsVO> pageInfo = new PageInfo<>(commentsVos);
        return pageInfo;
    }

    @Override
    public ResultVO getCommentsCountByProductId(String productId) {
        //1.查询当前商品的总记录数
        /**
         * Example\Example.Criteria必须新建，才能保证查询的独立性
         */
        Example exampleTotal = new Example(ProductComments.class);
        Example.Criteria criteriaTotal = exampleTotal.createCriteria();
        criteriaTotal.andEqualTo("product_id",productId);
        int count = productCommentsMapper.selectCountByExample(exampleTotal);
        //2.查询好评评价数
        Example exampleGood = new Example(ProductComments.class);
        Example.Criteria criteriaGood = exampleGood.createCriteria();
        criteriaGood.andEqualTo("product_id",productId);
        criteriaGood.andEqualTo("comm_type",1);
        int goodTotal = productCommentsMapper.selectCountByExample(exampleGood);

        Example exampleMid = new Example(ProductComments.class);
        Example.Criteria criteriaMid = exampleMid.createCriteria();
        criteriaMid.andEqualTo("product_id",productId);
        criteriaMid.andEqualTo("comm_type",0);
        int midTotal = productCommentsMapper.selectCountByExample(exampleMid);

        //4.查询差评评价数
        Example exampleBad = new Example(ProductComments.class);
        Example.Criteria criteriaBad = exampleBad.createCriteria();
        criteriaBad.andEqualTo("product_id",productId);
        criteriaBad.andEqualTo("comm_type",-1);
        int badTotal = productCommentsMapper.selectCountByExample(exampleBad);

        //5.查询好评率
        double goodPercent = (Double.parseDouble(goodTotal+"")/Double.parseDouble(count+"")) * 100;
        String percentValue = (goodPercent+"").substring(0,(goodPercent+"").lastIndexOf(".")+2);
        HashMap<String,Object> map = new HashMap<>();
        map.put("total",count);
        map.put("goodTotal",goodTotal);
        map.put("midTotal",midTotal);
        map.put("badTotal",badTotal);
        map.put("goodPercent",percentValue);

        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,map);
    }

}
